package com.chelun.support.clmutifragment.delegates;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chelun.support.clmutifragment.DisplayImageOptionsUtil;
import com.chelun.support.clmutifragment.R;
import com.chelun.support.clmutifragment.model.ImageBean;
import com.chelun.support.clmutiplyfragment.model.ListItemBean;
import com.hannesdorfmann.adapterdelegates3.AdapterDelegate;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by zhangguobao on 16/10/24.
 */
public class ImageAdapterDelegates extends AdapterDelegate<List<ListItemBean>> {
    @Override
    public boolean isForViewType(@NonNull List<ListItemBean> items, int position) {
        return items.get(position) instanceof ImageBean;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false);
        return new ImageHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull List<ListItemBean> items, int position, @NonNull RecyclerView.ViewHolder holder, @NonNull List<Object> payloads) {
        ImageHolder viewHolder = (ImageHolder) holder;
        ImageBean bean = (ImageBean) items.get(position);
        ImageLoader.getInstance().displayImage(bean.getUrl(),viewHolder.ivTest, DisplayImageOptionsUtil.getNormalImageOptions());
    }

    private class ImageHolder extends RecyclerView.ViewHolder {

        private final ImageView ivTest;

        public ImageHolder(View view) {
            super(view);
            ivTest = (ImageView) view.findViewById(R.id.ivTest);
        }
    }
}
